package com.qf.test;

import com.qf.dao.UserMapper;
import com.qf.pojo.TbUser;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
 *@Description:
 *@author:刘哥
 *@Date:86180 2022/8/23 16:33
 */
public class Demo02 {
    private SqlSession sqlSession;
    private UserMapper mapper;
    @Before
    public void before() throws IOException {

        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader("mybatis.xml"));
        sqlSession=sessionFactory.openSession();
        mapper=sqlSession.getMapper(UserMapper.class);

    }
    @After
    public  void  commit(){
        sqlSession.commit();
        sqlSession.close();
    }
    @Test
    public  void  selectAll(){
        List<TbUser> tbUser =mapper.selectAll();
        System.out.println("tbUser = " + tbUser);
    }
    @Test
    public  void  selectTbUser(){
        TbUser tbUser = mapper.selectUserName("zhangsan", "123");
        System.out.println("tbUser = " + tbUser);
    }
    @Test
    public  void  selectLike(){
        List<TbUser> tbUser = mapper.selectLike("a");
        System.out.println("tbUser = " + tbUser);
    }
    @Test
    public  void  Add(){
        TbUser tbUser = new TbUser();
        tbUser.setUsername("申志恒");
        tbUser.setPassword("123456");
        tbUser.setEmail("46354564@qq.com");
        tbUser.setGender("男");
        mapper.add(tbUser);
        System.out.println("tbUser.getId() = " + tbUser.getId());
    }
    @Test
    public  void  Map(){
        Map map=new HashMap();
        map.put("username","申志恒");
        map.put("password","123456");
        map.put("email","46354564@qq.com");
        map.put("gender","男");
        mapper.selectMap(map);

    }
    @Test
    public  void  delete(){
        int delete = mapper.delete(14L);
        System.out.println("delete = " + delete);

    }
    @Test
    public  void  update(){
        Map map=new HashMap();
        map.put("username","申志恒2号");
        map.put("password","654321");
        map.put("gender","女");
        map.put("id",15L);
        mapper.updateUser(map);

    }

}
